package org.sinuto.documentserver.controller;

import org.sinuto.documentserver.bean.Document;
import org.sinuto.documentserver.bean.EditorConfig;
import org.sinuto.documentserver.constant.Error;
import org.sinuto.documentserver.exception.DocumentServerException;
import org.sinuto.documentserver.service.CommandService;
import org.sinuto.documentserver.service.DocumentServerService;
import org.sinuto.documentserver.service.FileService;
import org.sinuto.documentserver.utils.CustomMap;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @Author 2023/2/27 9:02 ly
 **/
@Controller
public class DocumentServerController {

    @Resource
    private DocumentServerService documentService;
    @Resource
    private CommandService commandApiService;
    @Resource
    private FileService fileService;


    @GetMapping("/pagination")
    @ResponseBody
    public Map pagination() {
        return CustomMap.build(2)
                .pu1("data", fileService.pagination())
                ;
    }

    @ResponseBody
    @PostMapping(value = "upload")
    public Map upload(@RequestParam("file") MultipartFile file) {
        try {
            fileService.upload(file);
        } catch (DocumentServerException e) {
            e.printStackTrace();
            return CustomMap.build(2)
                    .pu1("msg", e.getMessage())
                    .pu1("code", e.getCode())
                    ;
        }

        return CustomMap.build(2)
                .pu1("msg", Error.SUCCESS.getMsg())
                .pu1("code", Error.SUCCESS.getCode())
                ;
    }


    @PostMapping("/edit")
    public String editDocFile(@RequestParam String id, String user, Model model) {
        Document document;
        try {
            document = documentService.buildDocument(id);
        } catch (DocumentServerException e) {
            e.printStackTrace();
            model.addAttribute("error", e.getMessage());
            return "/error";
        }

        EditorConfig editorConfig = documentService.buildEditorConfig(id, user);
        model.addAttribute("document", document);
        model.addAttribute("editorConfig", editorConfig);

        return "/editor";
    }

    @GetMapping("/download")
    public void download(@RequestParam String id, HttpServletResponse response) {
        try {
            documentService.download(id, response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 编辑文档时回调接口
     *
     * @param request
     * @param response
     * @throws IOException
     */
    @PostMapping("/callback")
    public void callback(HttpServletRequest request, HttpServletResponse response) {
        //处理编辑回调逻辑
        try {
            documentService.callBack(request, response);
        } catch (DocumentServerException | IOException e) {
            e.printStackTrace();
        }
    }


    @GetMapping("/force-save")
    @ResponseBody
    public Map<String, Object> forceSave(@RequestParam String id) throws DocumentServerException {
        return CustomMap.build(1).pu1("data", commandApiService.forceSave(id));
    }

    @GetMapping("/drop")
    @ResponseBody
    public Map drop(@RequestParam String id, String user) throws DocumentServerException {
        return CustomMap.build(1).pu1("data", commandApiService.drop(id, user));
    }

    @GetMapping("/info")
    @ResponseBody
    public Map info(@RequestParam String id) throws DocumentServerException {
        return CustomMap.build(1).pu1("data", commandApiService.info(id));
    }

    @GetMapping("/license")
    @ResponseBody
    public Map license() throws DocumentServerException {
        return CustomMap.build(1).pu1("data", commandApiService.license());
    }

    @GetMapping("/meta")
    @ResponseBody
    public Map meta(@RequestParam String id) throws DocumentServerException {
        return CustomMap.build(1).pu1("data", commandApiService.meta(id));
    }

    @GetMapping("/version")
    @ResponseBody
    public Map version() throws DocumentServerException {
        return CustomMap.build(1).pu1("data", commandApiService.version());
    }

}
